<?php

namespace App\Http\Controllers\Web\Buyer;

use App\ConstDir\ErrorConst;
use App\Http\Controllers\Web\BaseController;
use App\Repository\Models\ReportType;
use App\Repository\Repositories\Interfaces\ReportRepository;
use App\Repository\Repositories\Interfaces\ReportTypeRepository;
use Illuminate\Http\Request;
use Prettus\Repository\Criteria\RequestCriteria;

/**
 * 举报管理
 *
 * @package App\Http\Controllers\Report
 */
class ReportController extends BaseController
{
	/**
	 * @var ReportRepository
	 */
	protected $report_repo;
	protected $user_id;
	protected $user_name;

	public function __construct(ReportRepository $report_repo, Request $request)
	{
		parent::__construct();
		$this->report_repo = $report_repo;
		$this->user_id = array_get($this->user, 'id');
		$this->user_name = array_get($this->user, 'name');
	}

	public function index()
	{
		$report = $this->report_repo->scopeQuery(function($sql){
			return $sql->where('user_id',$this->user_id);
		})->paginate($this->perPage);
		return $this->success(ErrorConst::SUCCESS_CODE, $report, true);
	}

	public function create(ReportTypeRepository $reporttype){

		$type=$reporttype->paginate($this->perPage,['id','name']);
		return $this->success(ErrorConst::SUCCESS_CODE, $type, true);

	}

	public function store(ReportTypeRepository $reporttype,Request $request)
	{

		$data = $request->all();
		$data['user_id']=$this->user_id;
		$data['user_name']=$this->user_name;
		$data['status']=0;
		$type=$reporttype->find($data['report_type_id'],['name']);
		$data['report_type']=$type['data']['name'];
		$ret = $this->report_repo->create($data);
		if ($ret) {
			return $this->success(ErrorConst::SUCCESS_CODE, ErrorConst::SUCCESS_CODE_MSG);
		} else {
			return $this->error(ErrorConst::FAILED_CODE);
		}
	}

	public function delete($id)
	{
		//删除现在没有，不实现
//		$ret = $this->change_repo->delete($id);
//
//		if ($ret) {
//			return $this->success(ErrorConst::SUCCESS_CODE, ErrorConst::SUCCESS_CODE_MSG);
//		} else {
//			return $this->error(ErrorConst::FAILED_CODE);
//		}
	}
}
